package com.quectel.business.common.scheduling;

import cn.hutool.core.lang.func.LambdaUtil;
import com.quectel.core.module.dept.dto.DeptPersonDto;
import com.quectel.core.module.dept.service.DeptPersonService;
import com.quectel.core.module.scheduling.dto.SchedulingAttendanceGroupHeadBindingDto;
import com.quectel.core.module.scheduling.service.SchedulingAttendanceGroupHeadBindingService;
import com.quectel.core.module.scheduling.service.SchedulingAttendanceGroupPersonBindingService;
import com.quectel.core.module.scheduling.service.SchedulingAttendanceGroupService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhangheng
 * @email nolan.zhang@quectel.com
 * @date 2023/7/10 14:29
 * @description 假期相关
 */
@Component
@Slf4j
public class SchedulingPersonBusiness {

    @DubboReference
    private SchedulingAttendanceGroupService schedulingAttendanceGroupService;
    @DubboReference
    private SchedulingAttendanceGroupPersonBindingService personBindingService;
    @DubboReference
    private SchedulingAttendanceGroupHeadBindingService headBindingService;
    @DubboReference
    private DeptPersonService deptPersonService;

    /**
     * 通过名称查看考勤负责人ids
     * @param name
     * @return
     */
    public List<Long> headPersonIds (Long tenantId ,String name){
        List<Long> deptPersonIds = new ArrayList<>();

        // 人名查人id
        Map<String,Object> deptPersonMap = new HashMap<>();
        deptPersonMap.put(LambdaUtil.getFieldName(DeptPersonDto::getTenantId), tenantId);
        deptPersonMap.put(LambdaUtil.getFieldName(DeptPersonDto::getName), name);
        List<DeptPersonDto> deptPersonDtos = deptPersonService.queryList(deptPersonMap);

        if (CollectionUtils.isNotEmpty(deptPersonDtos)){
           deptPersonIds = deptPersonDtos.stream()
                    .map(DeptPersonDto::getId)
                    .collect(Collectors.toList());
        }

        List<Long> headBindings = new ArrayList<>();

        if (CollectionUtils.isNotEmpty(deptPersonIds)){
            Map<String,Object> headPersonMap = new HashMap<>();
            headPersonMap.put("personIds", deptPersonIds);

            // 人id查考勤配置id
            List<SchedulingAttendanceGroupHeadBindingDto> schedulingAttendanceGroupHeadBindingDtos =
                    headBindingService.queryListWithoutPadding(headPersonMap);

            if (CollectionUtils.isNotEmpty(schedulingAttendanceGroupHeadBindingDtos)){
                headBindings = schedulingAttendanceGroupHeadBindingDtos.stream()
                        .map(SchedulingAttendanceGroupHeadBindingDto::getSchedulingAttendanceGroupId)
                        .collect(Collectors.toList());
            }
        }
        return headBindings;
    }
}
